// pages/car/car.js

const { cartInfo,modifyNumber,remove } = require('../../utils/http')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    totalPrice:0,
  },
  getTotalPrice(content){
    var price = 0
    content.forEach(item=>{
      price += item.price*item.number
    })
    this.setData({
      totalPrice:price
    })
  },
  numberPlus(e){
    const that = this
    const token = wx.getStorageSync('token')
    const info = e.currentTarget.dataset.operation
    const {key,number} = info
    const queryInfo = {}
    queryInfo.token = token
    queryInfo.key = key
    queryInfo.number = number+1
    modifyNumber(queryInfo).then(res=>{
      that.firstReload()
    })
  },
  numberMinus(e){
    const that = this
    const token = wx.getStorageSync('token')
    const info = e.currentTarget.dataset.operation
    const {key,number} = info
    const queryInfo = {}
    queryInfo.token = token
    queryInfo.key = key
    queryInfo.number = number-1
    modifyNumber(queryInfo).then(res=>{
      that.firstReload()
    })
  },
  changeNumber(e){
    const that = this
    const number = e.detail
    const token = wx.getStorageSync('token')
    const info = e.currentTarget.dataset.operation
    const queryInfo = {}
    queryInfo.token = token
    queryInfo.key = info.key
    queryInfo.number = number
    modifyNumber(queryInfo).then(res=>{
      that.firstReload()
    })
  },
  delet(e){
    const that = this
    const token = wx.getStorageSync('token')
    const info = e.currentTarget.dataset.operation
    const queryInfo = {}
    queryInfo.token = token
    queryInfo.key = info.key
    console.log(queryInfo)
    remove(queryInfo).then(res=>{
      that.firstReload()
    })
  },

  firstReload(){
    const that = this
    const token = wx.getStorageSync('token')
    cartInfo(token).then(res=>{
      that.getTotalPrice(res.data.items)
      that.setData({
        goodsList:res.data.items
      })
      console.log(that.data.goodsList)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.firstReload()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})